package com.probiz.estore.system.service.impl;

import java.util.Date;

import com.probiz.estore.common.helper.ConfigUtil;
import com.probiz.estore.common.model.system.AppRole;
import com.probiz.estore.common.model.system.AppUser;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.core.util.StringUtil;
import com.probiz.estore.system.dao.AppUserDao;
import com.probiz.estore.system.service.AppRoleManager;
import com.probiz.estore.system.service.AppUserManager;


/**
 * Manager implementation for AppUser, responsible for business processing, and communicate between web and persistence layer.
 */
public class AppUserManagerImpl<T extends AppUser> extends GenericManagerImpl<T> implements AppUserManager<T> {

	protected AppUserDao<T> appUserDao = null;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
	}

	protected AppRoleManager appRoleManager = null;

	
	public void setAppRoleManager(AppRoleManager appRoleManager) {
		this.appRoleManager = appRoleManager;
	}

	public boolean getIsUserExist(String username) {	
		return appUserDao.isUserExist(username);
	}
	
	public boolean getIsEmailUseByOthers(String email, Integer myAppuserId) {  
	        return appUserDao.getIsEmailUseByOthers(email,myAppuserId);
    }
	public boolean getIsEmailExist(String email){
	    return appUserDao.isEmailExist(email);
	}
	
	public T getUserByName(String username) {
        return appUserDao.getUserByName(username);
    }
	
	public void saveAppUserStatusByEmail(String email, Short status) {
		appUserDao.saveAppUserStatusByEmail(email,status);
    }
	
	public boolean getIsPasswordRight(Integer appuserId,String oldPassword){
		oldPassword = StringUtil.encodePassword(oldPassword, ConfigUtil.getInstance().getPasswordEncryptionAlgorithm());
		T appUser=this.getById(appuserId);
		if(appUser.getPassword().equals(oldPassword)){
			return true;
		}else{
			return false;
		}
	}
	
	public void saveModifyPassword(Integer appuserId,String newPassword){
		appUserDao.modifyPasswordByAppuserId(appuserId,newPassword);
	}
    
	public void saveModifyPassword(String email, String newPassword) {
		appUserDao.modifyPasswordByEmail(email,newPassword);
    }
	
	public void updateRolesByAppuserId(String roleIdList, Integer appuserId) {
		
		T appUser=this.getById(appuserId);
		//clear old roles
		appUser.getUserRoles().clear();
		
		if(!roleIdList.trim().equals("")){
			String[] roleIds=roleIdList.trim().split("[,]");
			
			if(roleIds.length>0){
				for(int i=0;i<roleIds.length;i++){
					AppRole appRole= appRoleManager.getById(new Integer(roleIds[i]));
					appUser.addRole(appRole);//add new roles
				}
			}
		}
		this.save(appUser);
	}

	public void saveLastLoginTimeByAppuserId(Integer appuserId,
            Date lastLoginTime) {
		appUserDao.saveLastLoginTimeByAppuserId(appuserId,lastLoginTime);
    }

	@Override
	protected void preDelete(T entity) {
		
	}

	@Override
	protected void preSave(T entity) {
		
	}
	
}
